import Taro from '@tarojs/taro'

// import { Component } from 'react'

// encryptedData: "EgiwRH0OID9TYKBg6u4ajDfGD780rrjN6au4Cv4hYN9P6P455iec6qCplSxEu5Fh6f6NQ80zlQ6xBeMj87CWXuvI8fE0H0YZmaMxKBdD70/Fg5cOH0FH4J80zDv0PhrbHZ0eCrv0CUa9Qm+Mv9+npF3+3Q6rB7sVjjJ9lRp00bRKQIdjBkdDXvRcWYc9jdiTbXcL3J8TZVajvrnOQwZo77kdoyf6/3zkW1tOSTbagiFFYkZ6eaG21pHi7ZOGA61vodFyySOKEjJa530thUoy2QdyClbrscCHu27eDyrFBtw0PC3Vjog5QHllSZ+JskuRmoC4GOeHwH2L0tkgRSSKEPJbR4YG+5jx512UCPbKE6D4XWhaRQqwLqEHO4yiw6lvlPbJc28B23mFPYtdZuzwRER7hDpNAhy5ytkMSF6XNRbHNHuJH8sydtV3/LzbEfNQa+mFJXPOYEtmM9ngbH8v1AxyroDpywLxkJdpJFwsTfs="
// errMsg: "getUserInfo:ok"
// iv: "BrKZvDh25x9i7JXRxbTyIA=="
// rawData: "{"nickName":"跟太阳说早安","gender":0,"language":"zh_CN","city":"","province":"","country":"","avatarUrl":"https://thirdwx.qlogo.cn/mmopen/vi_32/BAwjJCfsjUH2TtaWEHU8wpxvF9Sib6YjD3hpicEQp8NaTRwIz4ib5DzqTYG6wpRsPAWUhjVnSthVSvtpV9G8jSicVg/132"}"
// signature: "e6622cbfebf6efe8a1fbfb804ca9b52d40955651"
// userInfo:
// avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/BAwjJCfsjUH2TtaWEHU8wpxvF9Sib6YjD3hpicEQp8NaTRwIz4ib5DzqTYG6wpRsPAWUhjVnSthVSvtpV9G8jSicVg/132"
// city: ""
// country: ""
// gender: 0
// language: "zh_CN"
// nickName: "跟太阳说早安"
// province: ""

const getUserInfo = async () => {

    // 首先获取本地是否有token
    try {
      const value = Taro.getStorageSync('token')
      if (value) {
        // Do something with return value
        return true
      }
    } catch (e) {
      // Do something when catch error
      console.log('获取本地token错误');
      return false
    }


    let userInfo = {
      code: '',
      userInfo: '',
      nickName: '',
      avatarUrl: '',
      gender: '', //性别 0：未知、1：男、2：女
      province: '',
      city: '',
      country: ''
    }

    // 获取code
    Taro.login({
      async success(response) {
        console.log('login success', response);
        userInfo.code = response.code
        await Taro.getUserInfo({
          success: function (res) {
            userInfo = { ...userInfo, ...res.userInfo }

            Taro.request({
              url: 'http://127.0.0.1:7001/certificate',
              data: {
                ...userInfo
              },
              method: "POST",
              success: function (restoken) {
                console.log("restoken",restoken,restoken.data.code);
                
                Taro.setStorage({
                  key: "token",
                  data: restoken.data.code
                })
                return true
              },
              fail(err) {
                console.log('失败返回的信息', err);
              }
            })
          }
        })
      },
      fail(err) {
        console.log('login error', err);
        return false
      }
    })
    // 必须是在用户已经授权的情况下调用
  }

  export default getUserInfo


